package cn.com.self.apis.util;

import org.apache.commons.lang3.ArrayUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
public class ParameterUtil {
   
    /**
     * request.getParameterMap 转Map<Stirng,Object>
     *
     * @param parameterMap     request.getParameterMap
     * @param ignoreProperties 需要忽略的属性数组
     * @return
     */
    public static Map<String, Object> wrapObjectMap(Map<String, String[]> parameterMap, String... ignoreProperties) {
        Map<String, Object> objectMap = new HashMap<>();
        parameterMap.forEach((key, value) -> {
            if (null != ignoreProperties && ArrayUtils.contains(ignoreProperties, key)) {
                return;
            } else {
                if (null != value) {
                    if (value instanceof String[]) {
                        String[] strs = value;
                        if (1 < strs.length) {
                            List<String> list = new ArrayList<>();
                            for (String str : strs) {
                                list.add(str);
                            }
                            objectMap.put(key, list);
                        } else {
                            objectMap.put(key, strs[0]);
                        }
                    } else {
                        objectMap.put(key, value);
                    }
                } else {
                    objectMap.put(key, null);
                }
            }
        });
        return objectMap;
    }


    public static void main(String[] args) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Map<String, String[]> stringMap = new HashMap<>();
        stringMap.put("1", new String[]{"1", "2", "3"});
        stringMap.put("2", new String[]{"f", "2s"});
        stringMap.put("3", new String[]{"f"});
        stringMap.put("3", new String[]{"f"});
        System.out.println(wrapObjectMap(stringMap, "3"));
    }
}
